package com.sunnypower.rushfitapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sunnypower.rushfitapp.util.GetData;
import com.sunnypower.rushfitapp.util.Saveuserinfo;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText et_number;
    private EditText et_password;
    private Button btn_login;
    private Button btn_reg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rushfit_login);
        //初始化页面
        init();
        //加载保存成功的用户信息
        Map<String, String> userInfo = Saveuserinfo.getUserInfo(this);
        if (userInfo != null) {
            et_number.setText(userInfo.get("number"));
            et_password.setText(userInfo.get("password"));
        }

    }
    private void init() {
        et_number = (EditText) findViewById(R.id.et_number);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login = (Button) findViewById(R.id.bt_login);
        btn_reg = (Button) findViewById(R.id.bt_reg);
        //设置按钮的点击事件
        btn_login.setOnClickListener(this);
        btn_reg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login://登录;

                //当点击登录按钮时,获取账号和密码
                String username = et_number.getText().toString().trim();
                String password = et_password.getText().toString().trim();

                String data= GetData.doGet("http://120.24.24.141:10990/rushfit/login2app?username="+username+"&password="+password);
                System.out.println(data);

                //检验账号和密码是否正确
                if (TextUtils.isEmpty(username)) {
                    Toast toast = onCardClick(getApplicationContext(), "请输入账号", Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }else if (TextUtils.isEmpty(password)) {
                    Toast toast = onCardClick(getApplicationContext(), "请输入密码", Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }

                if(data.equals("nouser")){
                    et_number.setText("");
                    et_password.setText("");
                    Toast toast = onCardClick(getApplicationContext(), "账号不存在", Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }else if(data.equals("success")){//登录成功进入主页
                    //保存用户信息 以便下一次用户登录不用输入
                    boolean isSaveSuccess = Saveuserinfo.saveUserInfo(this, username, password);
                    if (isSaveSuccess) {

                    } else {
                        Toast toast = onCardClick(getApplicationContext(), "保存失败", Toast.LENGTH_SHORT);
                        toast.show();
                    }

                    //获取uid
                    String uid= GetData.doGet("http://120.24.24.141:10990/rushfit/getuid2app?username="+username);
                    System.out.println(uid);
                    //登录成功进入主页
                    //把username传递给个人中心页面：
                    Intent intent = new Intent(MainActivity.this, VipMainActivity.class);
                    intent.putExtra("uid", uid);
                    intent.putExtra("username", username);
                    //resultCode  1代表成功，0代表失败：
                    setResult(1,intent);
                    startActivity(intent);
                    Toast toast = onCardClick(getApplicationContext(), "欢迎登录，用户:"+username, Toast.LENGTH_SHORT);
                    toast.show();
                    return;
                }

                 if(data.equals("error")){
                    et_password.setText("");
                     Toast toast = onCardClick(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT);
                     toast.show();
                    return;
                }
                break;
            case R.id.bt_reg://注册
                Intent intent = new Intent(MainActivity.this, RegActivity.class);
                startActivity(intent);
                Toast toast = onCardClick(getApplicationContext(), "欢迎注册", Toast.LENGTH_SHORT);
                toast.show();
                break;
        }
    }

    public Toast onCardClick(Context context, CharSequence text, int duration) {
        Toast toast = Toast.makeText(context, text, duration);
        toast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
        return toast;
    }
}
